// merchants/provide/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponList: [],
    oldCouponList: [],
    createUid: 0,
    isChecked: false,
    checkNumber: 0,
    inputMoney: ''
    
  },

  // 获取我领取的优惠券
  getCouponList: function(createUid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCouponList?type=1&uid=' + app.globalData.userInfo.Uid + "&createUid=" + createUid,
      success: function(res) {
        if (res.data.Code == 0) {
          let couponList = res.data.Data.CouponList;
          for (let i = 0; i < couponList.length; i++) {
            couponList[i].usestarttime = couponList[i].usestarttime.substr(0, 10);
            couponList[i].useendtime = couponList[i].useendtime.substr(0, 10);
            couponList[i].checkNumber = 0;
            couponList[i].isSelected = 0;
          }
          that.setData({
            couponList: couponList,
            oldCouponList: couponList,
          })
        } else {
          wx.redirectTo({
            url: '/pages/weijieke/cardList/index',
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.createUid) {
      this.getCouponList(options.createUid);
      this.setData({
        createUid: options.createUid
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  select: function(e) {
    let that = this;
    var couponList = that.data.couponList;
    let couponid = e.currentTarget.dataset.couponid;
    for (var i = 0; i < couponList.length; i++) {
      if (couponid == couponList[i].couponid) {
        if (couponList[i].isSelected) {
          couponList[i].isSelected = 0;
        } else {
          couponList[i].isSelected = 1;
        }
      }
    }
    this.setData({
      couponList: couponList
    })
  },
  // 减
  numJianTap: function(e) {
    let that = this;
    var couponList = that.data.couponList;
    let couponid = e.currentTarget.dataset.couponid;
    for (var i = 0; i < couponList.length; i++) {
      if (couponid == couponList[i].couponid) {
        if (couponList[i].checkNumber > 0) {
          couponList[i].checkNumber--;
        }
      }
    }
    this.setData({
      couponList: couponList
    })
  },
  // 加
  numJiaTap: function(e) {
    let that = this;
    var couponList = that.data.couponList;
    let couponid = e.currentTarget.dataset.couponid;
    for (var i = 0; i < couponList.length; i++) {
      if (couponid == couponList[i].couponid) {
        if (couponList[i].checkNumber < couponList[i].Counts) {
          couponList[i].checkNumber++;
        }
      }
    }
    this.setData({
      couponList: couponList
    })

  },
  bindnumber: function(e) {
    let that = this;
    var couponList = that.data.couponList;
    let couponid = e.currentTarget.dataset.couponid;
    for (var i = 0; i < couponList.length; i++) {
      if (couponid == couponList[i].couponid) {
        couponList[i].checkNumber = e.detail.value;
        if (couponList[i].checkNumber > 0){
          couponList[i].isSelected = 1;
        }
      }
    }
    this.setData({
      couponList: couponList
    })
  },
  inputMoney: function(e) {
    let inputMoney = e.detail.value;
    let that = this;
    let couponList = that.data.oldCouponList;
    let couponList2 = that.data.couponList;
    let shengyuMoney = inputMoney;
    for (let i = 0; i < couponList.length; i++) {
      if (shengyuMoney >= couponList[i].orderamountlower && couponList[i].orderamountlower > 0) {
        couponList[i].checkNumber = parseInt(shengyuMoney/couponList[i].orderamountlower);
        if (couponList[i].checkNumber>0){
          couponList[i].isSelected = 1;
        }
        if (couponList[i].checkNumber > couponList[i].Counts) {
          couponList[i].checkNumber = couponList[i].Counts;
        } else if (couponList[i].checkNumber < 1){
          couponList[i].checkNumber = 0;
        }
        shengyuMoney = parseFloat(shengyuMoney) - parseFloat(couponList[i].orderamountlower * couponList[i].checkNumber);
      } else if (couponList[i].orderamountlower == 0){
        couponList[i].checkNumber = couponList[i].Counts;
        couponList[i].isSelected = 1;
      }
      else {
        break;
      }
    }
    that.setData({
      inputMoney: inputMoney,
      couponList: couponList
    })
  }
})